import {createRouter, createWebHistory} from 'vue-router'
import VueCookies from "vue-cookies";

const routes =[
        {
            path: '/',
            name: 'home',
            component: () => import('@/views/home/index.vue'),
            children: [
                {
                    path: '/',
                    name: 'index',
                    component: () => import('@/views/Index/index.vue')
                },
                {
                    path: '/v/:pCategoryCode',
                    name: 'categoryVideo',
                    component: () => import('@/views/videoList/CategoryVideo.vue')
                },
                {
                    path: '/v/:pCategoryCode/:categoryCode',
                    name: 'subCategoryVideo',
                    component: () => import('@/views/videoList/CategoryVideo.vue')
                },
                {
                    path: '/video/:videoId',
                    name: 'videoDetail',
                    component: () => import('@/views/videoDetail/videoDetail.vue')
                }
            ]
        },
        {
            path: '/ucenter',
            name: 'ucenter',
            redirect: '/ucenter/home',
            component: () => import('@/views/ucenter/index.vue'),
            children: [
                {
                    path: '/ucenter/home',
                    name: '用户中心首页',
                    component: () => import('@/views/ucenter/home.vue')
                },
                {
                    path: '/ucenter/upload',
                    name: '上传操作',
                    redirect: '/ucenter/upload/postVideo',
                    component: () => import('@/views/ucenter/PostVideo/upload.vue'),
                    children: [
                        {
                            path: '/ucenter/upload/postVideo',
                            name: '上传视频',
                            component: () => import('@/views/ucenter/PostVideo/postVideo.vue')
                        }
                    ]
                },
                {
                    path: '/ucenter/editVideo',
                    name: '编辑视频',
                    component: () => import('@/views/ucenter/PostVideo/editVideo.vue')
                }
            ]
        }
    ]

// 创建router实例
const router = createRouter({
    history: createWebHistory(),
    routes,
});

router.beforeEach((to, from, next) => {
    if(to.path === '/ucenter/home' && !VueCookies.isKey('token')){
        next('/');
    }
    next();
});

export default router